package LeetCode.month01.day30;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class No2831 {
    public int longestEqualSubarray(List<Integer> nums, int k) {
        int n = nums.size();
        List<Integer>[] list=  new ArrayList[n+1];
        Arrays.setAll(list,i->new ArrayList<>());
        for (int i = 0; i < nums.size(); i++) {
            int index = nums.get(i);
            list[index].add(i-list[index].size());
        }
        int res = 0;
        for (List<Integer> integers : list) {
            int left = 0;
            if(integers.size() <= res){
                continue;
            }
            for (int i = 0; i < integers.size(); i++) {
                while(integers.get(i)-integers.get(left)>k){
                    left++;
                }
                res = Math.max(res,i-left+1);
            }
        }
        return res;
    }
}
